package com.retailshop.shopfinace.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.retailshop.shopfinace.entity.PostJson;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

public class FinaceVer {

    /**
     * 接口验证
     * @param request
     * @param jsonParam
     * @return
     */
    public static Map<String, Object> Security(HttpServletRequest request, @RequestBody JSONObject jsonParam) {
        Map<String, Object> map = new HashMap<>();
        //验证IP
        String IP = UserRequest.getIp(request);
        System.out.println(IP);
        //if (Config.ipz.contains(";"+ IP +";")) {
        if(1==1){
            // 直接将json信息打印出来
            System.out.println("JSON："+jsonParam.toJSONString());

            //将接收到的数据转化为对象
            PostJson postJson = JSON.parseObject(jsonParam.toJSONString(), new TypeReference<PostJson>() {
            });
            //验证密钥
            String modKey = postJson.getModKey();
            String key = Encrypt.decrypt(modKey);
            if (key.equals(Config.key)) {
                // 直接将json信息打印出来
                System.out.println("密钥:" + key);
                map.put("code","000000");
                map.put("AID",postJson.getAID());
                map.put("UID",postJson.getUID());
                map.put("parm",postJson.getParm());
            }
            else
            {
                //KEY错误
                map.put("code","666011");
            }
        }
        else {
            //IP不被容许
            map.put("code","666012");
        }
        return map;
    }

}
